package com.joelemon.mockinfluxdbdata.mock.service;

import com.joelemon.mockinfluxdbdata.common.SystemException;
import com.joelemon.mockinfluxdbdata.mock.ConnectConfig;
import com.joelemon.mockinfluxdbdata.mock.DataLine;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 数据库插入服务
 * @Author: HJY
 * @Date: 2023/11/20
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DefaultDBInsertService {

    private final DBInsertServiceFactory insertServiceFactory;

    /**
     * 插入数据
     * @param connectConfig
     * @param dataLines
     * @param insertType
     * @return
     */
    public int insert(ConnectConfig connectConfig, List<DataLine> dataLines, String insertType) {
        if (insertType == null || insertType.length() == 0) insertType = DBInsertServiceFactory.HTTP;
        DBInsertService defaultService = insertServiceFactory.getService(insertType);
        if (defaultService == null) {
            throw new SystemException("未找到插入服务:" + insertType);
        }
        return defaultService.insertData(connectConfig, dataLines);
    }
}
